package eleveController;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BeanQuestions;
import model.BeanQuestionsRepondues;
import model.Question;
import model.QuestionRepondue;
import model.Reponse;
import model.Utilisateur;

/**
 * Servlet implementation class ServletFrontEleve
 */
@WebServlet("/ServletFrontEleve")
public class ServletFrontEleve extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletFrontEleve() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		
		// initialisation avec la 1ere question du qcm
		ServletContext context = request.getSession().getServletContext();
		BeanQuestions questionsChoisies = (BeanQuestions) context.getAttribute("questionsChoisies");
		HttpSession session = request.getSession(true);
		
		Utilisateur utilisateurCourant = (Utilisateur)session.getAttribute("Usersession");
		
		//si l'utilisateur est loger et que c'est un prof on lui donne accés à l'espace prof, sinon on le renvoi sur la page d'authentification
		if(utilisateurCourant != null)
		{
			if(questionsChoisies==null){
				RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/homeEleve.jsp");
				dispatcher.forward(request,response);
			}
			else{
				
				Question questionCourante = questionsChoisies.getFirstQuestion();
				session.setAttribute("questionCourante", questionCourante);
				BeanQuestionsRepondues mesReponses = new BeanQuestionsRepondues();
				session.setAttribute("mesReponses", mesReponses);
				//Vector<QuestionRepondue> toutesMesReponsesChoisies = new Vector<QuestionRepondue>();
				//session.setAttribute("toutesMesReponsesChoisies", toutesMesReponsesChoisies);
				
				if(questionCourante == null){
					RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/homeEleve.jsp");
					dispatcher.forward(request,response);
				}
				else{
					RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/questionsEleve.jsp");
					dispatcher.forward(request,response);
				}
			}
		}
		else
		{
			String strDispatch = "/Test_Projet_QCM/Authentification";
			response.sendRedirect(strDispatch);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		ServletContext context = request.getSession().getServletContext();
		BeanQuestions mesQuestionsChoisies = (BeanQuestions) context.getAttribute("questionsChoisies");
		
		HttpSession session = request.getSession(true);
		Question questionCourante = (Question) session.getAttribute("questionCourante");
		BeanQuestionsRepondues toutesMesReponses = (BeanQuestionsRepondues) session.getAttribute("mesReponses");
		
		QuestionRepondue mesReponsesChoisies = new QuestionRepondue(questionCourante); 	

		
		
		if(request.getParameter("envoyer") != null){
			
			    
//			String reponseval = null;
//			= request.getParameter("question_"+questionCourante.getId());
//			System.out.println("R�ponse : "+request.getParameter("question_"+questionCourante.getId()));
			
			
			
			
			//LISA
			
			Vector<Reponse> reponses = questionCourante.getListeReponses();
			Vector<Reponse> reponsesEleveQuestionCourante = new Vector<Reponse>();
			

			mesReponsesChoisies.setQuestion(questionCourante);
			mesReponsesChoisies.setReponsesChoisies(null);	//Initialisation au cas ou l'eleve ne reponde pas
			
			for(int i=0;i<reponses.size();i++){
//				System.out.println(reponses.get(i).getIntitule()+" : ");
				if((request.getParameter("question_"+questionCourante.getId())).equals(""+i))
				{
					reponsesEleveQuestionCourante.add(reponses.elementAt(i));
//					System.out.println("Coch�e");					
				}
//				else
//					System.out.println("Non coch�e");
//				if(reponseval != null) {
//					mesReponsesChoisies.addReponse(reponses.get(i));
//				}				
			}
			
			if(reponsesEleveQuestionCourante!=null) // Si l'�l�ve a r�pondu
				mesReponsesChoisies.setReponsesChoisies(reponsesEleveQuestionCourante);
//				System.out.println("--->"+mesReponsesChoisies.);
			
			
			//Resultat � g�rer avec BeanQuestionsRepondues
			toutesMesReponses.add(mesReponsesChoisies);
			session.setAttribute("mesReponses", toutesMesReponses);
			
			//gestion resultat plusieurs eleve
			Vector<BeanQuestionsRepondues> ttesLesReponses = (Vector<BeanQuestionsRepondues>) context.getAttribute("ttesLesReponses");
			ttesLesReponses.add(toutesMesReponses);
			context.setAttribute("ttesLesReponses", ttesLesReponses);
			
			
			//Vector<QuestionRepondue> toutesMesReponsesChoisies = ((Vector<QuestionRepondue>) session.getAttribute("toutesMesReponsesChoisies"));
			//toutesMesReponsesChoisies.add(mesReponsesChoisies);
			//session.setAttribute("toutesMesReponsesChoisies", toutesMesReponsesChoisies);
			
//			int finFormulaire = 0;
			if(mesQuestionsChoisies.getQuestionSuivante(questionCourante)!=null){
				session.setAttribute("questionCourante", mesQuestionsChoisies.getQuestionSuivante(questionCourante));
//				finFormulaire = 1;
				RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/questionsEleve.jsp");
				dispatcher.forward(request,response);
			}
			else{
				//LISA
//				if(finFormulaire == 0){
//				if(finFormulaire != 0){
//				
//					RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/homeEleve.jsp");
//					dispatcher.forward(request,response);
//				}
//				else{
//				System.out.println("Fin du QCM");
					RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/resultatsEleve.jsp");
					dispatcher.forward(request,response);
//				}
				//EO LISA
			}	
			//EOLISA
		}
		//else{  //AUDE
//			if(request.getParameter("suivant") != null){
//				int finFormulaire = 0;
//				if(mesQuestionsChoisies.getQuestionSuivante(questionCourante)!=null){
//					session.setAttribute("questionCourante", mesQuestionsChoisies.getQuestionSuivante(questionCourante));
//					finFormulaire = 1;
//					RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/questionsEleve.jsp");
//					dispatcher.forward(request,response);
//				}
//				else{
//					if(finFormulaire == 0){
//						RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/homeEleve.jsp");
//						dispatcher.forward(request,response);
//					}
//					else{
//						RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/resultatsEleve.jsp");
//						dispatcher.forward(request,response);
//					}
//				}
//			}
	
		/*on enleve le retour puisque il fausse les resultats
		 *
		 *else if(request.getParameter("retour") != null){
				RequestDispatcher dispatcher = request.getRequestDispatcher("eleve/homeEleve.jsp");
				dispatcher.forward(request,response);
		}*/
			
		//LISA
		else{
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request,response);
		}
			//EO LISA
			
	}
}
